package com.zhongxiangspace.configuration;

import com.zhongxiangspace.config.MyUserInfoVerify;
import com.zhongxiangspace.filter.MyOncePerRequestFilter;
import com.zhongxiangspace.filter.MySessionListener;
import com.zhongxiangspace.filter.ShiroLoginFilter;
import org.apache.shiro.mgt.SessionsSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.mgt.SecurityManager;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfiguration {
    /**
     * ShiroFilterFactoryBean 处理拦截资源文件问题。
     * 注意：单独一个ShiroFilterFactoryBean配置是或报错的，以为在
     * 初始化ShiroFilterFactoryBean的时候需要注入：SecurityManager
     *
     Filter Chain定义说明
     1、一个URL可以配置多个Filter，使用逗号分隔
     2、当设置多个过滤器时，全部验证通过，才视为通过
     3、部分过滤器可指定参数，如perms，roles
     *   注意：引入SecurityManager 是 rg.apache.shiro.mgt.SecurityManager；
     */

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
        System.out.println("------shiroFilter---->");
        ShiroFilterFactoryBean shiroFilterFactoryBean  = new ShiroFilterFactoryBean();
        // 必须设置 SecurityManager
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //拦截器.
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();
        //配置退出过滤器,其中的具体的退出代码Shiro已经替我们实现了
        filterChainDefinitionMap.put("/logout.do", "logout");
        //<!-- 过滤链定义，从上向下顺序执行，一般将
        // /**放在最为下边 -->:这是一个坑呢，一不小心代码就不好使了;
        //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问-->
        filterChainDefinitionMap.put("/util/adminLoginnCaptcha.do", "anon");
        filterChainDefinitionMap.put("/memberLoin.do", "anon");
        filterChainDefinitionMap.put("/**", "authc");
        // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面
      shiroFilterFactoryBean.setLoginUrl("/login.do");
        // 登录成功后要跳转的链接
        shiroFilterFactoryBean.setSuccessUrl("/index.do");
        //未授权界面;
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean(name = "mySessionListener")
    public MySessionListener mySessionListener(){
        MySessionListener mySessionListener = new MySessionListener();
        return mySessionListener;
    }



    @Bean(name = "shiroLoginFilter")
    public ShiroLoginFilter shiroLoginFilter(){
        System.out.println("---shiroLoginFilter-------->");
        ShiroLoginFilter shiroLoginFilter = new ShiroLoginFilter();
        return shiroLoginFilter;
    }

    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
        securityManager.isHttpSessionMode();
        securityManager.setRealm(myShiroRealm());
        return securityManager;
    }




    @Bean
    public MyUserInfoVerify myShiroRealm(){
        MyUserInfoVerify myShiroRealm = new MyUserInfoVerify();
        return myShiroRealm;
    }
}
